﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace ClientApplication
{
    public partial class Login : Form, ILogin
    {
        public event LoginEventHandler ConnectClicked = null;

        public Login()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Connect_Button_Click(object sender, EventArgs e)
        {
            Connect();
        }
        
        private void Connect()
        {
            try
            {
                IPAddress serverIP = IPAddress.Parse(IP_textBox.Text);
                LoginEventArgs ev = new LoginEventArgs(ID_textBox.Text, Password_textBox.Text, serverIP);
                if (this.ConnectClicked != null)
                    this.ConnectClicked(this, ev);
            }
            catch
            {
                MessageBox.Show("Wrong IP-adress-format");
            } 
        }

        private void ID_testBox_TextChanged(object sender, EventArgs e)
        {
            CheckBoxes();
        }

        private void Password_testBox_TextChanged(object sender, EventArgs e)
        {
            CheckBoxes();
        }
        private void CheckBoxes()
        {
            if ((ID_textBox.Text.Trim().Length > 0 && Password_textBox.Text.Trim().Length > 0 && IP_textBox.Text.Trim().Length > 0))
            {
                Connect_Button.Enabled = true;
            }
            else
            {
                Connect_Button.Enabled = false;
            }
        }

        private void Password_textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
                if (Connect_Button.Enabled == true)
                    Connect();
        }
    }
}
